<?php

namespace IDock\Insurance\LIHI\Response;

use IDock\Http\Response;
use IDock\Insurance\LIHI\Response\AllLIHIResponse;
use IDock\Printer\PolicyPaymentPrinter;

class PolicyPaymentResponse extends AllLIHIResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);

        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }
        $status = $data['success'] ?? '';
        $resultMess = $data['result'] ?? '';
        if(!$status) {
            $this->setError($resultMess);
            return $this;
        }
        $this->setBody($data);

        return $this;
    }

    /**
     * @var PolicyPaymentPrinter $printer
     */
    public $printer = PolicyPaymentPrinter::class;

    public function printerInit(): Response
    {
        $data = $this->getBody();
        $this->printer->payUrl = $data['result'] ?? '';
        return $this;
    }
}
